package patrol.mobile.com.mobilepatrol.activity.yanghuke.zuoyeshangbao;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;

import com.dl7.recycler.adapter.BaseQuickAdapter;
import com.dl7.recycler.adapter.BaseViewHolder;
import com.dl7.recycler.listener.OnRecyclerViewItemClickListener;
import com.google.gson.Gson;

import java.util.HashMap;

import butterknife.BindView;
import okhttp3.Call;
import patrol.mobile.com.mobilepatrol.R;
import patrol.mobile.com.mobilepatrol.activity.CommonListWithSearchActivity;
import patrol.mobile.com.mobilepatrol.activity.yanghuke.zuoyeshangbao.bean.ZuoyeListBean;
import patrol.mobile.com.mobilepatrol.api.Urls;
import patrol.mobile.com.mobilepatrol.manager.UserManager;
import patrol.mobile.com.mobilepatrol.utils.net.HttpCallBack;
import patrol.mobile.com.mobilepatrol.utils.net.HttpSender;

/**
 * Created by z on 2018/9/12.
 * Des: 作业List页面
 */

public class ZuoyeListActivity extends CommonListWithSearchActivity {
    private ZuoyeQuickAdapter zuoyeQuickAdapter;
    private ZuoyeListBean zuoyeListBean;
    @BindView(R.id.titleAddIcon)
    public ImageView titleAddIcon;

    @Override
    protected BaseQuickAdapter initAdapter() {
        return zuoyeQuickAdapter = new ZuoyeQuickAdapter(this);
    }

    @Override
    public void initData() {
        super.initData();
        zuoyeQuickAdapter.setOnItemClickListener(new OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                startActivity(new Intent(view.getContext(), ZuoyeDetailActivity.class)
                        .putExtra("zuoyeListBean", zuoyeListBean.getList().get(position).getId()));
            }
        });
        titleAddIcon.setVisibility(View.VISIBLE);
        titleAddIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(v.getContext(), ZuoyeDetailActivity.class));
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        getHttpData();
    }

    /**
     * 获取作业区信息列表接口
     */
    private void getHttpData() {
        HashMap<String, String> hashMap = new HashMap();
        hashMap.put("orgId", UserManager.getOrgNo());
        HttpSender sender = new HttpSender(this, baseUrl, Urls.getYhkOperationAreaList);
        sender.requestPost(hashMap, new HttpCallBack(sender) {
            @Override
            public void onError(String code, Call call, Exception e) {

            }

            @Override
            public void onResponse(String code, String response) {
                zuoyeListBean = new Gson().fromJson(response, ZuoyeListBean.class);
                zuoyeQuickAdapter.updateItems(zuoyeListBean.getList());
            }
        });
    }

    @Override
    protected String initTitleName() {
        return "作业区信息列表";
    }

    public class ZuoyeQuickAdapter extends BaseQuickAdapter<ZuoyeListBean.ListBean> {

        public ZuoyeQuickAdapter(Context context) {
            super(context);
        }

        @Override
        protected int attachLayoutRes() {
            return R.layout.item_zuoyefenbu_layout;
        }

        @Override
        protected void convert(BaseViewHolder holder, ZuoyeListBean.ListBean item) {
            holder.setText(R.id.item_num, (zuoyeListBean.getList().indexOf(item) + 1) + "");
            holder.setText(R.id.itmeTitle, item.getRoutelinename() + "  " + item.getPot());
            holder.setText(R.id.itemType, item.getOrgName() + "  " + item.getCuringStop());
            holder.getView(R.id.iv_loc).setVisibility(View.GONE);
            holder.getView(R.id.shengheType).setVisibility(View.VISIBLE);
            if (TextUtils.isEmpty(item.getWorksInfo())) {
                holder.setText(R.id.itemTypeContent, "");
            } else {
                holder.setText(R.id.itemTypeContent, item.getWorksInfo());
            }
            holder.setText(R.id.shengheType, item.getWorksType());
            holder.setText(R.id.itemTimeTextView, item.getUpdateDate());
        }
    }
}
